// pages/home/home.js
Page({
    data: {
        courses: [],
        searchResult: [],
        categories: [],
        selectedCourse: null // Add this to store the selected course
    },
    onLoad(options) {
        this.checkLoginStatus();
        if(options.orderStatus){
            this.setData({
                orderStatus: options.orderStatus
            });
        }
        if (!this.data.userId) {
            // 如果用户未登录，导航到 login 页面
            wx.navigateTo({
                url: '/pages/login/login'
            });
        } else {
            this.getOrders(); // 页面加载时只获取类别数据
        }
    },
    navigateBack() {
        wx.navigateBack();
    },
    checkLoginStatus() {
        const userId = wx.getStorageSync('userId');
        if (userId) {
            this.setData({
                userId: userId
            });
        }
    },
    getOrders: function() {
        wx.request({
            url: 'http://127.0.0.1:5000//api/orders',
            method: 'GET',
            data: {
                user_id: this.data.userId,
                order_status: this.data.orderStatus
            },
            success: (res) => {
                this.setData({
                    orders: res.data.orders
                });
            },
            fail: (err) => {
                wx.showToast({
                    title: '获取订单失败',
                    icon: 'none'
                });
            }
        });
    },

    cancelOrder() {
        const orderId = this.data.paymentInfo.order_id; // 假设订单ID在 paymentInfo 中

        wx.request({
            url: `/api/orders/${orderId}/cancel`,
            method: 'POST',
            success: (res) => {
                if (res.statusCode === 200) {
                    console.log('订单取消成功:', res.data);
                    this.setData({
                        paymentInfo: null,
                        orderCancelInfo: '订单已取消'
                    });
                } else {
                    console.error('订单取消失败:', res.data);
                    this.setData({
                        orderCancelInfo: '订单取消失败'
                    });
                }
            },
            fail: (err) => {
                console.error('请求失败:', err);
                this.setData({
                    orderCancelInfo: '请求失败'
                });
            }
        });
    },
    getUserId() {
        // 实现获取用户ID的逻辑
        // 例如，从缓存中获取用户ID
        return wx.getStorageSync('userId');
    },

    handleSearch: function(e) {
        // 搜索逻辑不变
    },
 
    goToCourseDetails: function(e) {
        const course = e.currentTarget.dataset.course;
        console.log('Navigating to course details:', course); // 调试信息
        if (typeof course === 'object' && course !== null && course.category_id) {
            wx.navigateTo({
                url: `/pages/courseDetail/courseDetail?course_id=${course.course_id}&category_id=${course.category_id}`
            });
        } else {
            console.error('Invalid course object:', course); // 调试信息
            wx.showToast({
                title: '课程数据无效，请重试',
                icon: 'none'
            });
        }
    }
    
});